package zhuoyue.com.yanjiaohui.acticity;

import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import zhuoyue.com.yanjiaohui.R;
import zhuoyue.com.yanjiaohui.base.BaseActivity;
import zhuoyue.com.yanjiaohui.fragment.FunFragment;
import zhuoyue.com.yanjiaohui.fragment.HomeFragment;
import zhuoyue.com.yanjiaohui.fragment.MineFragment;
import zhuoyue.com.yanjiaohui.fragment.StoreFragment;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup mRadioGroup;

    //主页
    private HomeFragment mHomeFragment;
    //商品
    private StoreFragment mStoreFragment;
    //fun页面
    private FunFragment mFunFragment;
    //我的页面
    private MineFragment mMineFragment;
    //记录当前的fragment
    private Fragment mCurrentFragment;

    private FragmentManager mFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       //findviewById
        initView();
        mHomeFragment = new HomeFragment();
        mFragmentManager = getSupportFragmentManager();
        mFragmentManager.beginTransaction()
                .add(R.id.main_fragment,mHomeFragment,"HomeFragment")
                .commit();
        mCurrentFragment = mHomeFragment;
        //点击每一个按钮对应一个Fragment ，这样做的好处就是解耦
        //每个功能模块充分的解耦。
        mRadioGroup.setOnCheckedChangeListener(this);

    }

    private void initView() {
        mRadioGroup = (RadioGroup) findViewById(R.id.main_radio_group);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        mFragmentManager = getSupportFragmentManager();
        switch (checkedId) {
            case R.id.main_home:
                mFragmentManager.beginTransaction()
                        .hide(mCurrentFragment)
                        .show(mHomeFragment)
                        .commit();
                mCurrentFragment = mHomeFragment;
                break;
            case R.id.main_store:
                if (mStoreFragment == null) {
                    mStoreFragment = new StoreFragment();
                    mFragmentManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .add(R.id.main_fragment,mStoreFragment,"mStoreFragment")
                            .commit();
                }else {
                    mFragmentManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .show(mStoreFragment)
                            .commit();
                }
                mCurrentFragment = mStoreFragment;
                break;
            case R.id.main_fun:
                if (mFunFragment == null) {
                    mFunFragment = new FunFragment();
                    mFragmentManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .add(R.id.main_fragment,mFunFragment,"mFunFragment")
                            .commit();
                }else {
                    mFragmentManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .show(mFunFragment)
                            .commit();
                }
                mCurrentFragment = mFunFragment;

                break;
            case R.id.main_mine:

                if (mMineFragment == null) {
                    mMineFragment = new MineFragment();
                    mFragmentManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .add(R.id.main_fragment,mMineFragment,"mMineFragment")
                            .commit();
                }else {
                    mFragmentManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .show(mMineFragment)
                            .commit();
                }
                mCurrentFragment = mMineFragment;
                break;
        }
    }


}
